user data 생성

  • USER(UID , NAME, LASTDATE, STARTDATE);
  • GLOG(LOGID,UID, GAMEDATE, SCORE);
DROP TABLE IF EXISTS USER;
CREATE TABLE USER(
    UID CHAR(10) PRIMARY KEY,
    NAME VARCHAR(32),
    LASTDATE TIMESTAMP,
    STARTDATE DATETIME
);

DROP TABLE IF EXISTS GLOG;
CREATE TABLE GLOG(
    LOGID INT PRIMARY KEY AUTO_INCREMENT,
    UID CHAR(10),
    GAMEDATE DATETIME,
    SCORE INT,
    FOREIGN KEY (UID) REFERENCES USER(UID)
);

In [9]:
import random
import string

def genID(n):
    str = ''.join(random.choices(string.ascii_uppercase + string.digits, k=n))
    return str

In [10]:
l = "김이박정최정강조윤장임한호서신권황"
m = "동해물과백두산이마르고도록하느님이보우하사우리나라만세무궁화삼천리화려강산대한사람"
def genName():
    return random.choice(l) + "".join(random.choices(m, k=2))

In [11]:
import random
import time

def strTimeProp(start, end, format, prop):
    stime = time.mktime(time.strptime(start, format))
    etime = time.mktime(time.strptime(end, format))

    ptime = stime + prop * (etime - stime)

    return time.strftime(format, time.localtime(ptime))


def randomDate(start, end, prop):
    return strTimeProp(start, end, '%Y/%m/%d %H:%M:%S', prop)

def genDate():
    return randomDate("2015/1/1 9:30:00", "2018/8/9 17:00:00", random.random())

In [12]:
record = "{},{},{},{}".format(genID(10), genName(),genDate(), genDate())
print(record)


Z9CQXEYS4Y,정리두,2016/09/15 04:04:42,2015/03/20 02:24:20

In [13]:
r1 = 20000
with open("user.csv","w") as f:
    for i in range(r1):
        record = "{},{},{},{}\n".format(genID(10), genName(),genDate(), genDate())
        f.write(record)

In [14]:
!pwd


/Users/honux/github/practice/python/csvgen

LOAD USER FILE FROM CSV


In [15]:
import pandas as pd

In [16]:
df = pd.read_csv('./user.csv', header=None,
                 names=['ID','NAME','LAST','START'])

In [17]:
random.choice(df['ID'])


Out[17]:
'QUSBBJCKRP'

In [18]:
random.choice(df['ID'])


Out[18]:
'O7F7BKEO3A'

Gen GLG

  • GLOG(LOGID,UID, GAMEDATE, SCORE);

In [19]:
random.randint(1000,1000000)


Out[19]:
374082

In [20]:
r2 = 3000000
with open('glog.csv','w') as gl:
    for i in range(r2):
        uid = random.choice(df['ID'])
        date = genDate()
        score = random.randint(1000,1000000)
        record = ",{},{},{}\n".format(uid,date,score)
        gl.write(record)

In [21]:
!ls


glog.csv                   user.csv
simple csv generator.ipynb

In [22]:
## 기록

- 그냥 load: 22.45
- set autocommit=0, unique_checks=0,


  File "<ipython-input-22-23b2acda31e1>", line 3
    - 그냥 load: 22.45초
            ^
SyntaxError: invalid syntax